package gui;
import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.SQLException;

import info.User;
import sql.Connect;

public class Frame extends JFrame {
    private CardLayout cardLayout;
    private JPanel mainPanel;
    public Frame(Connect conn) {
        setTitle("请假系统");
        Toolkit kit =Toolkit.getDefaultToolkit();
        Dimension screenSize = kit.getScreenSize();
        setSize(screenSize.width/2, screenSize.height/2);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setResizable(false);

        //创建面板
        cardLayout = new CardLayout();
        mainPanel = new JPanel(cardLayout);
     addWindowListener(new WindowAdapter() {
                @Override
                public void windowClosing(WindowEvent e) {
                    // 关闭数据库连接
                    try {
                        conn.close(); // 调用连接关闭方法
                    } catch (SQLException ex) {
                        throw new RuntimeException(ex);
                    }
                    System.exit(0);
                }
     });
        mainPanel.add(new Login().createLogin(cardLayout,mainPanel,this,conn), "Login");
        mainPanel.add(new Register().creatRegister(cardLayout,mainPanel,conn), "Register");
        add(mainPanel);
    }



    public void main(Connect conn ) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Frame(conn).setVisible(true);
            }
        });

    }

    }

